# -*- coding: utf-8 -*-
import scrapy

from BI_Spider.items import BiSpiderItem


class TencentSpider(scrapy.Spider):
    name = 'tencent'
    allowed_domains = ['tencent.com']

    base_url = "https://hr.tencent.com"
    page_url = base_url + '/position.php?&start='

    offset = 0
    start_urls = [page_url + str(offset)]

    custom_settings = {
        'ITEM_PIPELINES': {'BI_Spider.pipelines.BiSpiderPipeline': 300},
    }

    def parse(self, response):
        # //tr[@class='even'] or //tr[@class='odd']
        node_list = response.xpath("//tr[@class='even'] | //tr[@class='odd']")

        for node in node_list:
            item = BiSpiderItem()

            # 这里是 ./  是基于上面节点获取的 相当于  //tr[@class='even']/td[1]/a/text() | //tr[@class='odd']/td[1]/a/text()
            # 提取字符串，并将编码转换为UTF 8
            # print(node.xpath("./td[1]/a/text()").extract()[0])

            item['positionName'] = node.xpath("./td[1]/a/text()").extract()[0]
            item['positionLink'] = node.xpath("./td[1]/a/@href").extract()[0]
            item['positionType'] = node.xpath("./td[2]/text()").extract()[0]
            item['peopleNumber'] = node.xpath("./td[3]/text()").extract()[0]
            item['workLocation'] = node.xpath("./td[4]/text()").extract()[0]
            item['publishTime'] = node.xpath("./td[5]/text()").extract()[0]
            yield item

        # 下一页
        if self.offset < 20:
            self.offset += 10
            url = self.page_url + str(self.offset)
            yield scrapy.Request(url, callback=self.parse)
